package org.zuel.huhuForum.util;

import lombok.extern.log4j.Log4j2;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;


@Log4j2
public class CookieUtil {

    // 从 request 中获取指定 name 的 cookie
    public static String getValue(HttpServletRequest request, String name) {
        if (request == null || name == null) {
            throw new IllegalArgumentException("参数为空");
        }

        Cookie[] cookies = request.getCookies();
        if(cookies != null) {
            for(Cookie cookie : cookies) {
                if (cookie.getName().equals(name)) {
//                    log.info(cookie.getName()+":"+cookie.getValue());
                    return cookie.getValue();
                }
            }
            log.info("未发现目标cookie...");
        }
        return null;
    }
}
